package com.pactera.madp.cp.handler;

import com.pactera.madp.cp.common.constant.Constant;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 企业id处理类
 */
@Component
public class CorpContextHoldlerFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String corpId = request.getHeader(Constant.MAPD_COPR_ID);
        if(!StringUtils.isEmpty(corpId)){
            CorpContextHolder.setCorpId(Integer.parseInt(corpId));
        }
        filterChain.doFilter(request, response);
        CorpContextHolder.remove();
    }

}
